package com.example.ck.service.impl;

import com.example.ck.dao.SmslogDao;
import com.example.ck.dto.PhoneCodeDto;
import com.example.ck.service.SmslogService;
import com.example.ck.utils.AliSmsUtil;
import com.example.ck.utils.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SmslogServiceImpl implements SmslogService {

    @Autowired
    private SmslogDao smslogDao;

    @Override
    public int sendCode(String phone) {
        // 校验是否有值
        if (StringUtils.hasLength(phone)) {
            // 正则校验
            if (phone.matches("^1[3-9][0-9]{9}$")) {
                // 生成验证码
                int code = NumUtil.createCode(4);
                // 发送验证码
                int i = AliSmsUtil.sendSmsCode(phone, Integer.toString(code));
                // 返回验证码
                return code;
            }
        }
        return 0;
    }

    @Override
    public int createSuccess(String phone, String name) {
        // 校验是否有值
        if (StringUtils.hasLength(phone)) {
            // 正则校验
            if (phone.matches("^1[3-9][0-9]{9}$")) {
                // 发送短信
                int i = AliSmsUtil.sendSmsCreate(phone, name);
                return i;
            }
        }
        return 0;
    }

}
